package com.lujieni.myredis.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lujieni.myredis.config.serializer.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Package: com.lujieni.myredis.config
 * @ClassName: RedisCongfiguration
 * @Author: lujieni
 * @Description: Redis配置
 * @Date: 2021-02-04 14:03
 * @Version: 1.0
 */
@Configuration
public class RedisCongfiguration {

    /**
     * @Description:
     *  方法名一定要叫 redisTemplate,否则 springboot仍旧会帮你生成 RedisTemplate
     *  详见RedisAutoConfiguration
     * @param
     * @return:
     * @Author: lujieni
     * @Date: 2021/2/4
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer();//将对象的类型也存入redis

        //redisTemplate.setValueSerializer(new StringRedisSerializer());如果存对象会报类型转换错误!!! Person -> String error

        /* 设置string的序列化方式 */
        redisTemplate.setKeySerializer(new StringRedisSerializer());//RedisTemplate默认使用jdk序列化方式
        redisTemplate.setValueSerializer(fastJsonRedisSerializer);

        redisTemplate.afterPropertiesSet();

        return redisTemplate;

    }

}